﻿package plugin.chatflash
{
    import __AS3__.vec.*;
    import cmd.*;
    import com.core.*;
    import com.core.utils.*;
    import com.greensock.*;
    import com.sh.game.consts.*;
    import com.sh.game.global.*;
    import com.sh.game.net.*;
    import com.sh.game.util.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.ui.*;
    import flash.utils.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.chatflash.controller.*;
    import plugin.chatflash.model.*;
    import plugin.newvip.model.*;
    import pluginfw.*;
    import view.mainUI.*;

    public class GameChat extends GameChatUI
    {
        private var mSelectedBtn:Button;
        public var mChannelControl:ChannelController;
        private var itemListString:String = "";
        private var mSendMsgList:Array;
        private var mLastMsgIndex:int = 0;
        private var mHeightChange:Boolean = false;
        private var _channelbtns:Vector.<Button>;
        private var isScale:Boolean = false;
        private var timeLine:TimelineLite;
        private var _face:FaceGame;
        private var _mouseOver:Boolean = false;
        private var _curChannelID:int = -1;
        private var _curchatID:int = 2;

        public function GameChat()
        {
            this.mSendMsgList = [];
            this._channelbtns = new Vector.<Button>;
            this._channelbtns.push(mBtnAll);
            this._channelbtns.push(mBtnShop);
            this._channelbtns.push(mBtnWorld);
            this._channelbtns.push(mBtnStage);
            this._channelbtns.push(mBtnTeam);
            this._channelbtns.push(mBtnUnion);
            this._channelbtns.push(mBtnPrivate);
            this._channelbtns.push(mBtnSystem);
            this.mChannelControl = new ChannelController(this);
            var _loc_1:* = Control.create(ChatFlashController, this);
            ModelProxy.addObserver("chat", _loc_1);
            this.listener();
            this.curChannelID = 0;
            setTimeout(this.checkChannelId, 3000);
            this.setPositions();
            this.mouseOutHandler();
            return;
        }// end function

        private function checkChannelId() : void
        {
            if (UserData.getUserData("level") <= 2)
            {
                this.curChannelID = 8;
            }
            return;
        }// end function

        private function listener() : void
        {
            mBtnSelect.addEventListener(MouseEvent.CLICK, this.showChannelListHandler);
            fuckScale.clickHandler = new Handler(this.doScale);
            mBtnAll.addEventListener(MouseEvent.CLICK, function () : void
            {
                changeChannelHandler(ChatCMD.ChannelAll);
                return;
            }// end function
            );
            mBtnStage.addEventListener(MouseEvent.CLICK, function () : void
            {
                changeChannelHandler(ChatCMD.ChannelStage);
                return;
            }// end function
            );
            mBtnTeam.addEventListener(MouseEvent.CLICK, function () : void
            {
                changeChannelHandler(ChatCMD.ChannelTeam);
                return;
            }// end function
            );
            mBtnUnion.addEventListener(MouseEvent.CLICK, function () : void
            {
                changeChannelHandler(ChatCMD.ChannelUnion);
                return;
            }// end function
            );
            mBtnWorld.addEventListener(MouseEvent.CLICK, function () : void
            {
                changeChannelHandler(ChatCMD.ChannelWorld);
                return;
            }// end function
            );
            mBtnPrivate.addEventListener(MouseEvent.CLICK, function () : void
            {
                changeChannelHandler(ChatCMD.ChannelPrivate);
                return;
            }// end function
            );
            mBtnShop.addEventListener(MouseEvent.CLICK, function () : void
            {
                changeChannelHandler(ChatCMD.ChannelShop);
                return;
            }// end function
            );
            mBtnSystem.addEventListener(MouseEvent.CLICK, function () : void
            {
                changeChannelHandler(ChatCMD.ChannelSystem);
                return;
            }// end function
            );
            mBtnPos.addEventListener(MouseEvent.CLICK, this.doSendMyPos);
            mBtnSend.addEventListener(MouseEvent.CLICK, this.sendMsgHandler);
            btn_face.addEventListener(MouseEvent.CLICK, this.faceHandler);
            GlobalLayer.instance.root.addEventListener(MouseEvent.CLICK, this._faceClose);
            mChatInput.addEventListener(FocusEvent.FOCUS_IN, this.focusInHandler);
            mChatInput.addEventListener(MouseEvent.CLICK, this.inputclickHandler);
            GlobalLayer.instance.root.addEventListener(KeyboardEvent.KEY_DOWN, this.keyDownHandler);
            addEventListener(MouseEvent.ROLL_OVER, this.mouseOverHandler);
            addEventListener(MouseEvent.ROLL_OUT, this.mouseOutHandler);
            GlobalLayer.instance.root.addEventListener(Event.RESIZE, this.resizeHandler);
            return;
        }// end function

        private function doScale() : void
        {
            if (this.isScale)
            {
                this.isScale = false;
                this.clearTween();
                this.timeLine = new TimelineLite();
                this.timeLine.insert(new TweenLite(fuckScale, 0.5, {y:38}));
                this.timeLine.insert(new TweenLite(mBG, 0.5, {y:0}));
                this.timeLine.insert(new TweenLite(panel_chat, 0.5, {y:0, height:145, onComplete:this.doComplete}));
                ModelProxy.change("data", "chatScale", false);
                ModelProxy.change("mainui", "chatScale", false);
            }
            else
            {
                this.isScale = true;
                this.clearTween();
                this.timeLine = new TimelineLite();
                this.timeLine.insert(new TweenLite(fuckScale, 0.5, {y:-102}));
                this.timeLine.insert(new TweenLite(mBG, 0.5, {y:-140}));
                this.timeLine.insert(new TweenLite(panel_chat, 0.5, {y:-140, height:285}));
                ModelProxy.change("data", "chatScale", true);
                ModelProxy.change("mainui", "chatScale", true);
            }
            return;
        }// end function

        private function clearTween() : void
        {
            if (this.timeLine)
            {
                this.timeLine.kill();
                this.timeLine = null;
            }
            return;
        }// end function

        private function doComplete() : void
        {
            panel_chat.scrollTo(0, 9999);
            return;
        }// end function

        protected function doSendMyPos(event:MouseEvent) : void
        {
            var _loc_2:* = new Object();
            var _loc_3:* = UserData.getUserData();
            var _loc_4:* = _loc_3.mapid;
            _loc_2.stagename = ConfigDictionary.data.maplist[_loc_4].name;
            _loc_2.x = _loc_3.x;
            _loc_2.y = _loc_3.y;
            _loc_2.stageid = _loc_4;
            ModelProxy.change("chat", "sendmypos", _loc_2);
            return;
        }// end function

        protected function faceOver(event:MouseEvent) : void
        {
            return;
        }// end function

        protected function inputclickHandler(event:MouseEvent) : void
        {
            GlobalLayer.instance.root.focus = mChatInput.textField;
            return;
        }// end function

        protected function faceHandler(event:MouseEvent) : void
        {
            event.stopPropagation();
            if (this._face)
            {
                this._face.visible = true;
            }
            else
            {
                this._face = new FaceGame(mChatInput);
                this._face.x = btn_face.x + btn_face.width;
                this._face.y = GlobalLayer.instance.root.stageHeight - this._face.height - 23;
                GlobalLayer.instance.root.addChild(this._face);
            }
            return;
        }// end function

        protected function _faceClose(event:MouseEvent) : void
        {
            if (this._face)
            {
                this._face.visible = false;
                this.checkHideBg();
            }
            if (mChannelList)
            {
                mChannelList.visible = false;
            }
            return;
        }// end function

        protected function mouseOutHandler(event:MouseEvent = null) : void
        {
            this._mouseOver = false;
            this.checkHideBg();
            return;
        }// end function

        protected function mouseOverHandler(event:MouseEvent) : void
        {
            this._mouseOver = true;
            this.checkHideBg();
            return;
        }// end function

        private function checkHideBg() : void
        {
            if (GlobalLayer.instance.root.focus == mChatInput.textField || this._mouseOver || this._face && this._face.visible)
            {
                TweenLite.to(fucklayer, 0.3, {alpha:1});
                panel_chat.vScrollBar.alpha = 1;
                GlobalLayer.instance.root.addEventListener(MouseEvent.CLICK, this._faceClose);
            }
            else
            {
                panel_chat.vScrollBar.alpha = 0.2;
                TweenLite.to(fucklayer, 0.3, {alpha:0.2});
            }
            return;
        }// end function

        private function resizeHandler(event:Event) : void
        {
            this.setPositions();
            return;
        }// end function

        private function setPositions() : void
        {
            this.y = GlobalLayer.instance.root.stageHeight - 228;
            UIVisibleManager.instance.register("chat", this, UIVisibleManager.VLeft);
            return;
        }// end function

        private function keyDownHandler(event:KeyboardEvent) : void
        {
            var _loc_2:TextField = null;
            var _loc_3:TextInput = null;
            switch(event.keyCode)
            {
                case Keyboard.ENTER:
                {
                    if (GlobalLayer.instance.root.focus == mChatInput.textField)
                    {
                        if (StringUtil.trim(mChatInput.text).length > 0)
                        {
                            this.sendMsgHandler(null);
                        }
                        else
                        {
                            GlobalLayer.instance.root.focus = null;
                        }
                        break;
                    }
                    else if (!GlobalLayer.instance.root.focus)
                    {
                        GlobalLayer.instance.root.focus = mChatInput.textField;
                    }
                    else if (GlobalLayer.instance.root.focus is TextField)
                    {
                        _loc_2 = GlobalLayer.instance.root.focus as TextField;
                        if (_loc_2.type != TextFieldType.INPUT)
                        {
                            GlobalLayer.instance.root.focus = mChatInput.textField;
                        }
                    }
                    else if (GlobalLayer.instance.root.focus is TextInput)
                    {
                        _loc_3 = GlobalLayer.instance.root.focus as TextInput;
                        if (!_loc_3.editable)
                        {
                            GlobalLayer.instance.root.focus = mChatInput.textField;
                        }
                    }
                    else
                    {
                        GlobalLayer.instance.root.focus = mChatInput.textField;
                    }
                }
                case Keyboard.DELETE:
                {
                    break;
                }
                case Keyboard.UP:
                {
                    if (GlobalLayer.instance.root.focus == mChatInput.textField && this.mLastMsgIndex < this.mSendMsgList.length)
                    {
                        mChatInput.text = this.mSendMsgList[this.mLastMsgIndex];
                        if (this.mLastMsgIndex < (this.mSendMsgList.length - 1))
                        {
                            var _loc_4:String = this;
                            var _loc_5:* = this.mLastMsgIndex + 1;
                            _loc_4.mLastMsgIndex = _loc_5;
                        }
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function focusInHandler(event:FocusEvent) : void
        {
            event.stopPropagation();
            if (mChatInput.text == DEFAULT_TEXT || mChatInput.text == STALLAGE_DEFAULT_TEXT)
            {
                mChatInput.text = "";
                mChatInput.addEventListener(FocusEvent.FOCUS_OUT, this.focusOutHandler);
            }
            GlobalLayer.instance.root.addEventListener(MouseEvent.CLICK, this.stageClickHandler);
            this.checkHideBg();
            return;
        }// end function

        private function focusOutHandler(event:FocusEvent) : void
        {
            if (mChatInput.text == "")
            {
                if (this.curChannelID == ChatCMD.ChannelShop)
                {
                    mChatInput.text = STALLAGE_DEFAULT_TEXT;
                }
                else if (this._curchatID == ChatCMD.ChannelPrivate)
                {
                    mChatInput.text = PRIVATE_DEFAULT_TEXT;
                }
                else
                {
                    mChatInput.text = DEFAULT_TEXT;
                }
                mChatInput.removeEventListener(FocusEvent.FOCUS_OUT, this.focusOutHandler);
            }
            this.checkHideBg();
            return;
        }// end function

        private function stageClickHandler(event:MouseEvent) : void
        {
            if (event.target == mChatInput.textField)
            {
                return;
            }
            GlobalLayer.instance.root.focus = null;
            GlobalLayer.instance.root.removeEventListener(MouseEvent.CLICK, this.stageClickHandler);
            return;
        }// end function

        private function shBtnRollOverHandler(event:MouseEvent) : void
        {
            var _loc_2:* = new Point(GlobalLayer.instance.root.mouseX, GlobalLayer.instance.root.mouseY);
            ModelProxy.change("tips", "normal", {data:"展开/关闭聊天框", p:_loc_2});
            return;
        }// end function

        private function shBtnRollOutHandler(event:MouseEvent) : void
        {
            ModelProxy.change("tips", "state", "close");
            return;
        }// end function

        private function showChannelListHandler(event:MouseEvent) : void
        {
            event.stopPropagation();
            mChannelList.show();
            return;
        }// end function

        private function changeChannelHandler(param1:int) : void
        {
            if (this.curChannelID != param1)
            {
                if (param1 == ChatCMD.ChannelPrivate && !mPCComponent.selectedRole)
                {
                    ModelProxy.change("movetips", "error", "请选择对象聊天");
                    return;
                }
                this.selectChannel(param1);
                this.curChannelID = param1;
                this.mChannelControl.display();
            }
            return;
        }// end function

        public function selectChannel(param1:int) : void
        {
            var _loc_2:int = 0;
            if (param1 == ChatCMD.ChannelPrivate && !mPCComponent.selectedRole)
            {
                ModelProxy.change("movetips", "error", "请选择对象聊天");
                return;
            }
            if (param1 != this.curChatID && param1 > 0 && param1 != 8)
            {
                if (param1 == ChatCMD.ChannelShop)
                {
                    mChatInput.text = STALLAGE_DEFAULT_TEXT;
                    mChatInput.mouseEnabled = false;
                    if (GlobalLayer.instance.root.focus == mChatInput.textField)
                    {
                        GlobalLayer.instance.root.focus = null;
                    }
                }
                else if (this._curchatID == ChatCMD.ChannelPrivate)
                {
                    mChatInput.text = PRIVATE_DEFAULT_TEXT;
                    mChatInput.mouseEnabled = true;
                }
                else
                {
                    mChatInput.text = DEFAULT_TEXT;
                    mChatInput.mouseEnabled = true;
                }
                mTextFormat.color = UIGlobel.chanel2color(param1);
                this._curchatID = param1;
                mBtnSelect.label = UIGlobel.chanelToname(param1);
                mBtnSelect.labelColors = UIGlobel.chanel2color(param1) + "," + UIGlobel.chanel2color(param1) + "," + UIGlobel.chanel2color(param1) + "," + UIGlobel.chanel2color(param1);
                mChatInput.textField.defaultTextFormat = mTextFormat;
                mChatInput.color = UIGlobel.chanel2color(param1);
                _loc_2 = 145;
                if (param1 == ChatCMD.ChannelPrivate)
                {
                    if (mPCComponent.visible == false)
                    {
                        mPCComponent.visible = true;
                        mChatInput.width = mChatInput.width - _loc_2;
                        mChatInput.x = mChatInput.x + _loc_2;
                    }
                }
                else if (mPCComponent.visible)
                {
                    mPCComponent.visible = false;
                    mChatInput.width = mChatInput.width + _loc_2;
                    mChatInput.x = mChatInput.x - _loc_2;
                }
            }
            return;
        }// end function

        private function showOpenDay() : void
        {
            var _loc_1:* = ModelProxy.getValue("data", "openday");
            ModelProxy.change("chat", "addsysmsg", "当前为开服第" + _loc_1 + " 天 (开服当天为0)");
            return;
        }// end function

        private function sendMsgHandler(event:MouseEvent) : void
        {
            var _loc_2:Object = null;
            var _loc_6:int = 0;
            var _loc_7:String = null;
            var _loc_8:Array = null;
            var _loc_9:String = null;
            var _loc_10:Object = null;
            var _loc_11:int = 0;
            var _loc_12:String = null;
            var _loc_13:ChatVO = null;
            if (this.curChatID == ChatCMD.ChannelShop)
            {
                return;
            }
            var _loc_3:* = mChatInput.text;
            this.mSendMsgList.unshift(_loc_3);
            this.mLastMsgIndex = 0;
            if (this.mSendMsgList.length > 10)
            {
                this.mSendMsgList.pop();
            }
            var _loc_4:* = ModelProxy.getValue("server", "onLogin")[4];
            if (_loc_3.charAt(0) == "@")
            {
                if (_loc_3.indexOf("openday") == 1)
                {
                    this.showOpenDay();
                    mChatInput.text = "";
                    return;
                }
                if (_loc_3 == "@showconsole")
                {
                    ModelProxy.change("plugin", "open", {target:"gm", limit:false});
                    return;
                }
                if (_loc_4.isgm > 0 || Config.debug)
                {
                    if (_loc_3.indexOf("@clientGm") >= 0)
                    {
                        ClientGm.doGm(_loc_3);
                        return;
                    }
                    if (_loc_3.indexOf("打开模块 ") == 1)
                    {
                        _loc_6 = _loc_3.indexOf(" ");
                        _loc_7 = _loc_3.toString().substr((_loc_6 + 1));
                        _loc_8 = _loc_7.split(",");
                        for each (_loc_9 in _loc_8)
                        {
                            
                            if (_loc_9 != "")
                            {
                                ModelProxy.change("plugin", "open", {target:_loc_9, limit:false});
                            }
                        }
                        return;
                    }
                    else if (_loc_3.indexOf("杀死目标") == 1 && _loc_3.length == 5)
                    {
                        _loc_10 = ModelProxy.getValue("data", "selectPlayer");
                        if (_loc_10 == null)
                        {
                            ModelProxy.change("movetips", "error", "请选择目标");
                            return;
                        }
                        _loc_3 = _loc_3 + (" " + _loc_10.id);
                    }
                    Server.instance.send(99, 0, _loc_3, 0, ConnectType.SYSTEM);
                }
                else
                {
                    ModelProxy.change("movetips", "error", "您不是GM，不能操作GM命令");
                }
                mChatInput.text = "";
                return;
            }
            else if (_loc_3.charAt(0) == "&")
            {
                _loc_11 = _loc_3.indexOf(" ");
                _loc_12 = "";
                if (_loc_11 > 0)
                {
                    _loc_12 = _loc_3.toString().substr(2);
                }
                GmTool.sendGmCMD(_loc_12);
                mChatInput.text = "";
                return;
            }
            var _loc_5:* = ModelProxy.getValue("data", "configs1.banednames");
            _loc_3 = ModelProxy.getValue("data", "configs1.banednames").replaceAll(_loc_3);
            _loc_3 = _loc_3.replace(/#!""#!/gi, "");
            if (_loc_3.length > 0 && _loc_3 != DEFAULT_TEXT && _loc_3 != STALLAGE_DEFAULT_TEXT)
            {
                _loc_13 = new ChatVO();
                _loc_13.Type = this.curChatID;
                _loc_13.RoleID = UserData.getUserData("rid");
                _loc_13.RoleName = UserData.getUserData("name");
                _loc_13.TeamID = UserData.getUserData("teamid");
                _loc_13.UnionID = UserData.getUserData("unionid");
                _loc_13.TalkContent = _loc_3;
                _loc_13.title = UserData.getUserData("title");
                _loc_13.itemList = this.itemListString;
                _loc_13.RoleVip = VipCheck.checkVip();
                if (this.curChatID == ChatCMD.ChannelPrivate && mPCComponent.selectedRole)
                {
                    _loc_13.ToRoleID = mPCComponent.selectedRole.rid;
                    _loc_13.ToRoleName = mPCComponent.selectedRole.name;
                    _loc_2 = UserData.getUserData();
                    _loc_13.sex = _loc_2.sex;
                    _loc_13.rein = _loc_2.rein;
                    _loc_13.level = _loc_2.level;
                    _loc_13.career = _loc_2.career;
                }
                if (this.curChatID == ChatCMD.ChannelUnion)
                {
                    _loc_2 = UserData.getUserData();
                    _loc_13.sex = _loc_2.sex;
                    _loc_13.career = _loc_2.career;
                }
                this.mChannelControl.sendMsg(_loc_13);
                mChatInput.text = "";
            }
            this.itemListString = "";
            return;
        }// end function

        private function clearDefaultInput() : void
        {
            if (mChatInput.text == STALLAGE_DEFAULT_TEXT || mChatInput.text == DEFAULT_TEXT)
            {
                mChatInput.text = "";
            }
            return;
        }// end function

        public function showItem(param1:Object) : void
        {
            this.itemListString = this.itemListString + (param1[4].itemtype + "," + param1[4].itemid + "," + param1[0] + ",");
            this.clearDefaultInput();
            mChatInput.appendText("[" + param1[4].name + "]");
            return;
        }// end function

        public function addName(param1:String) : void
        {
            this.clearDefaultInput();
            mChatInput.appendText(param1);
            return;
        }// end function

        public function privateChat(param1:Object) : void
        {
            mPCComponent.addRole(param1);
            this.selectChannel(ChatCMD.ChannelPrivate);
            return;
        }// end function

        public function get curChannelID() : int
        {
            return this._curChannelID;
        }// end function

        public function set curChannelID(param1:int) : void
        {
            var _loc_2:int = 0;
            if (this._curChannelID != param1)
            {
                _loc_2 = 0;
                while (_loc_2 < this._channelbtns.length)
                {
                    
                    if (_loc_2 == param1)
                    {
                        this._channelbtns[_loc_2].selected = true;
                        this._channelbtns[_loc_2].labelColors = "0xe19f21,0xe19f21,0xe19f21,0xe19f21";
                    }
                    else
                    {
                        this._channelbtns[_loc_2].labelColors = "0xb9774b,0xb9774b,0xb9774b,0xb9774b";
                        this._channelbtns[_loc_2].selected = false;
                    }
                    _loc_2++;
                }
                if (param1 == 8)
                {
                    this._channelbtns[7].selected = true;
                    this._channelbtns[7].labelColors = "0xe19f21,0xe19f21,0xe19f21,0xe19f21";
                }
                this._curChannelID = param1;
                if (param1 != 0 && param1 != 8)
                {
                    this.curChatID = param1;
                }
            }
            return;
        }// end function

        public function get curChatID() : int
        {
            return this._curchatID;
        }// end function

        public function set curChatID(param1:int) : void
        {
            this._curchatID = param1;
            mBtnSelect.label = UIGlobel.chanelToname(this._curchatID);
            mBtnSelect.labelColors = UIGlobel.chanel2color(this._curchatID) + "," + UIGlobel.chanel2color(this._curchatID) + "," + UIGlobel.chanel2color(this._curchatID) + "," + UIGlobel.chanel2color(this._curchatID);
            return;
        }// end function

    }
}
